package com.servlet;

import com.dao.LoginDao;
import com.dao.impl.LoginDaoimpl;
import com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 羡羡
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String uname = request.getParameter("uname");
        String upass = request.getParameter("pass");
        LoginDao dao = new LoginDaoimpl();
        User user = dao.login(uname,upass);
        if(user==null){
            response.getWriter().write("<script>alert('登录失败！请重试！');location.href='login.jsp'</script>");
        }
        request.getSession().setAttribute("uname",user.getUname());
        request.getSession().setAttribute("img",user.getHead());
        response.sendRedirect("MessageServlet/searchmessage");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
